/**
 * Affichage du tableau des medecins.
 */     
function displayDoctors() {
	
	var cs_division		= $('#activite-equipe-id').get(0).value; 
	var periodeActivite = $('#activite-periode-id').get(0).value;
	var specialite		= $('#activite-specialite-id').get(0).value;
	 
	$('#gridDoctors').empty(); 
	
	var itemsParPage = 24;  
	
	var store = Ext.create('Ext.data.Store', {
       id:			'gridDoctorsStore',
       autoLoad: 	false,
       fields:		['name', 'firstname', 'zipCode', 'city', 'spe', 'subtype', 'cible', 'nbCalls', 'dernVis', 'proVis'],
       pageSize: 	itemsParPage, 
       remoteSort: 	true,
       proxy: {
           type: 'ajax',
           url: './dataGridDoctors.do?itemsParPage=' + itemsParPage + '&nivGeog=' + globalNivGeog + '&idGeog=' + idGeog + "&cs_division=" + cs_division + "&periodeActivite=" + periodeActivite + "&specialite=" + specialite + "&segments=" + allSegments,   
           reader: {
               type: 			'json',
               root: 			'result.data',
               totalProperty: 	'result.total'
           }
       }
	});
	
	store.load({
       params:{
           start: 0,    
           limit: itemsParPage
       }
	});
	
	// Filtre 
	var filters = { 
        ftype:	'filters',
        encode:	true,
        local:	false,   
        filters: [{
            type:		'boolean',
            dataIndex:	'visible'
        }]
    };
	
    // Creation de la Grid 
	Ext.create('Ext.grid.Panel', { 
		viewConfig: {
			loadMask: false // permet de ne pas afficher l icone de chargement 
		},
		listeners: {
			viewready: function(){
				$('#loadGridDoctorsImg').css("display", "none");
				//this.store.load(); // sinon requete lancée deux fois "dataGridDoctors" 
            }
		},
		store: store,
		dockedItems: [{
           xtype: 'pagingtoolbar', 
           store: store,   // same store GridPanel is using
           dock: 'bottom'
	    }],
		columns: [
			{header: 'Nom',				width: '11%', dataIndex: 'name', 		sortable: true, align: 'left', filterable:true,
				renderer: function(value, metaData, record, row, col, store, gridView) {
					if (record.data.spe == 'PM' || record.data.spe == 'PZ' || record.data.spe == 'PG' 
						|| record.data.spe == 'PO' || record.data.spe == 'OH') { // Pharmacie 
						return "<img src='img/sante.png' style='padding-right:5px;' />&nbsp;&nbsp;" + value;
					} else { // Medecin - Specialiste - Hospitalier  
						if (record.data.subtype == 'Hospital') {
							return "<img src='img/croix.png' style='padding-right:5px;' />&nbsp;&nbsp;" + value;
						} else if (record.data.spe == 'MG') {
							return "<img src='img/doctor.png' style='padding-right:5px;' />&nbsp;&nbsp;" + value;
						} else {
							return "<img src='img/medecin.png' style='padding-right:5px;' />&nbsp;&nbsp;" + value;
						}
					}
				}
			},
			{header: 'Pr&eacute;nom',	width: '11%', dataIndex: 'firstname',	sortable: true, align: 'left', filterable:true},
			{header: 'Code postal',		width: '11%', dataIndex: 'zipCode',		sortable: true, align: 'left', filterable:true},
			{header: 'Ville',			width: '11%', dataIndex: 'city', 		sortable: true, align: 'left', filterable:true},
			{header: 'Sp&eacute;.',		width: '11%', dataIndex: 'spe', 		sortable: true, align: 'left', filterable:true},
			{header: 'Cible',			width: '11%', dataIndex: 'cible', 		sortable: true, align: 'left', filterable:true},
			{header: 'Nb calls/obj',	width: '11%', dataIndex: 'nbCalls', 	sortable: true, align: 'left', filterable:true},
			{header: 'Derni&egrave;re vis.',	width: '11%', dataIndex: 'dernVis',	sortable: true, align: 'left', filterable:true},
			{header: 'Prochaine vis.',			width: '11%', dataIndex: 'proVis', 	sortable: true, align: 'left', filterable:true}
		],
		features: [filters],
		stripeRows: true, 
		renderTo: 'gridDoctors',
		title:'<div style="float:left;">M&eacute;decins pour ' + globalGeog + '</div><div style="float:right;"><a href="#" onclick="javascript:downloadExcelMedecins();"><img src="img/excel-petit.png" /></a><a href="#" onclick="javascript:downloadPdfMedecins();"><img src="img/pdf-petit.png" /></a></div>'
	});
}

/**
 * Chargement du tableau récapitulatif des médecins à la France, à la zone, à la région ou au secteur. 
 */
function loadTabRecap() {
	var cs_division		= $('#activite-equipe-id').get(0).value; 
	var periodeActivite = $('#activite-periode-id').get(0).value;
	var specialite		= $('#activite-specialite-id').get(0).value;
	
	$.ajax({ 
 		traditional: true, 
 		type: 'POST', 
 		url: './recapDoctors.do',
 		target: "_blank", 
 		data : {
 			nivGeog:			globalNivGeog,
 			idGeog:				idGeog,
 			cs_division:		cs_division,
 			periodeActivite:	periodeActivite,
 			specialite:			specialite,
 			segments:			allSegments
 		}, 
 		error: function () {
 		},
 		success: function(result) { 
 			displayTabRecap(result.lines);
 		}
 	});
}

/**
 * Affichage du tableau récapitulatif des médecins à la France, à la zone, à la région ou au secteur.
 */
function displayTabRecap(lines) {
	
	$('#gridDoctors').empty(); 
	
	// Donnees JSON 
	var linesStore = new Ext.data.JsonStore({   
      fields: [ 'cible', 'nbCibles', 'nbVus', 'repartAct', 'nbMentions', 'couv', 'freqReelle', 'objFreq'],
      data: lines
    });  
	
	// Filtre 
	var filters = {
        ftype: 'filters',
        encode: false,
        local: true,   
        filters: [{
            type: 'boolean',
            dataIndex: 'visible'
        }]
    };
	
    // Creation de la Grid 
	Ext.create('Ext.grid.Panel', { 
		store: linesStore,
		columns: [
		    {header: '',									width: '8%',  dataIndex: 'cible', 		sortable: true, align: 'left', filterable:true},
			{header: 'Nombre Cibl&eacute;s',				width: '13%', dataIndex: 'nbCibles', 	sortable: true, align: 'left', filterable:true},
			{header: 'Nombre Vus',							width: '13%', dataIndex: 'nbVus',		sortable: true, align: 'left', filterable:true},
			{header: 'R&eacute;partition activit&eacute;',	width: '13%', dataIndex: 'repartAct', 	sortable: true, align: 'left', filterable:true},
			{header: 'Nombre Mentions',						width: '13%', dataIndex: 'nbMentions', 	sortable: true, align: 'left', filterable:true},
			{header: 'Couverture',							width: '13%', dataIndex: 'couv', 		sortable: true, align: 'left', filterable:true},
			{header: 'Fr&eacute;quence R&eacute;elle',		width: '13%', dataIndex: 'freqReelle', 	sortable: true, align: 'left', filterable:true},
			{header: 'Objectif Fr&eacute;quence',			width: '13%', dataIndex: 'objFreq', 	sortable: true, align: 'left', filterable:true}
		],
		features: [filters],
		stripeRows: true, 
		renderTo: 'gridDoctors',
		title:'<div style="float:left;">R&eacutecapitulatif</div>',
		listeners : {
			cellclick : function(grid, rowIndex, cellIndex, e){  
			},
			viewready: function(){
				$('#loadGridDoctorsImg').css("display", "none");
            }
		},
		viewConfig: { 
	        stripeRows: false, 
	        getRowClass: function(record) { 
	        	if (record.data.cible == 'A' || record.data.cible == 'C') {
	        		return 'ACCell';
	        	} else if (record.data.cible == 'B' || record.data.cible == 'D') {
	        		return 'BDCell';
	        	} else if (record.data.cible == 'Total cible') {
	        		return 'totalCell';
	        	} else {
	        		return 'hsCell';
	        	}
	        } 
	    } 
	});
}

/**
 * Chargement des données JSON pour l'affichage des medecins.
 */
function loadGridDoctors() {
	if (globalNivGeog == 'France' || globalNivGeog == 'Zone' || globalNivGeog == 'Region' || globalNivGeog == 'Secteur') {
		// Affichage du tableau recapitulatif 
		$('#gridDoctors').empty();
		$('#loadGridDoctorsImg').css("display", "none");
		loadTabRecap();
	} else {
		// Affichage de la liste des medecins 
		displayDoctors();
	}
}
